from django.db import models
from django.contrib.auth.models import User
#from django.contrib.admin.util import
#from django import forms
from django.forms import ModelForm, Textarea
from django.contrib.auth.forms import UserChangeForm
'''
class ContactForm(forms.Form):
    subject = forms.CharField(max_length=100)
    message = forms.CharField()
    sender = forms.EmailField()
    cc_myself = forms.BooleanField(required=False)
'''
class AbstractUser(models.Model):
    user = models.OneToOneField(User)
    friends = models.ManyToManyField('self', null=True)
    banned = models.BooleanField()
    banning_reason = models.CharField(max_length=1000)
    reviews_notification = models.IntegerField()
    connections_notification = models.IntegerField()
    messages_notification = models.IntegerField()
    
    
    def __unicode__(self):
        return self.user.username
    
class Teacher(models.Model):
    abstract_user = models.OneToOneField(AbstractUser)
    threads_recieved = models.ManyToManyField('Thread', null=True)
    photo = models.OneToOneField('Photo', null=True, on_delete=models.SET_NULL)
    #schedule = models.ForeignKey('Schedule', null=True, on_delete=models.SET_NULL)
    work_institutes = models.ManyToManyField('Institute') #because it is created later on, we use single quotations
    
    dob = models.DateTimeField('Date of birth', blank=True, null=True)
    gender = models.CharField(max_length=6, blank=True, choices=(('Male', 'Male'), ('Female', 'Female')))
    experience = models.CharField(max_length=1000, blank=True)
    favourite_genre = models.CharField(max_length=1000, blank=True)
    instrument_played = models.CharField(max_length=1000, blank=True)
    jamming_style = models.CharField(max_length=1000, blank=True) 
    teaches = models.CharField(max_length=1000, blank=True)
    HOF = models.BooleanField()
    category = 'teacher' # needed to identify type of object in generic templates
    reviews = models.IntegerField() # will be used for statistics on who is the highest reviewed. This is here instead of being calculated for optimization reasons
 
class Institute(models.Model):
    abstract_user = models.OneToOneField(AbstractUser)
    threads_recieved = models.ManyToManyField('Thread')
    logo = models.OneToOneField('Photo', null=True, on_delete=models.SET_NULL, related_name="logo")
    
    about = models.CharField(max_length=1000, blank=True)
    address = models.CharField(max_length=1000, blank=True)
    open_on = models.CharField(max_length=500, blank=True)
    open_from = models.TimeField('Open From (time - 24hr format)', blank=True, null=True)
    open_to = models.TimeField('Open To (time - 24hr format)', blank=True, null=True)
    contact_info = models.CharField(max_length=1000, blank=True)
    HOF = models.BooleanField()
    category = 'institute' # needed to identify type of object in generic templates
    reviews = models.IntegerField() # will be used for statistics on who is the highest reviewed. This is here instead of being calculated for optimization reasons
    
    def __unicode__(self):
        return self.abstract_user.user.first_name + " " + self.abstract_user.user.last_name
    
class Student(models.Model):
    abstract_user = models.OneToOneField(AbstractUser) 
    threads_recieved = models.ManyToManyField('Thread', null=True)
    photo = models.OneToOneField('Photo', null=True, on_delete=models.SET_NULL)
    
    dob = models.DateTimeField('Date of birth', blank=True, null=True)
    gender = models.CharField(max_length=6, blank=True, choices=(('Male', 'Male'), ('Female', 'Female')))
    experience = models.CharField(max_length=1000, blank=True)
    favourite_genre = models.CharField(max_length=1000, blank=True)
    instrument_played = models.CharField(max_length=1000, blank=True)
    jamming_style = models.CharField(max_length=1000, blank=True)
    HOF = models.BooleanField()
    category = 'student' # needed to identify type of object in generic templates
    reviews = models.IntegerField() # will be used for statistics on who is the highest reviewed. This is here instead of being calculated for optimization reasons
    
class ConnectionRequest(models.Model):
    sender = models.ForeignKey(AbstractUser, related_name='sender')
    receiver = models.ForeignKey(AbstractUser, related_name='receiver')
    category = models.CharField(max_length=6) # work or friend
    def __unicode__(self):
        return 'From ' + self.sender.__str__() + ' to ' + self.receiver.__str__()
       
class Photo(models.Model):
    institute = models.ForeignKey(Institute, null=True)
    path = models.CharField(max_length=200)
    caption = models.CharField(max_length=200)
    #image_file = models.ImageField(upload_to="uploaded/")  

class Ad(models.Model):
    photo = models.ForeignKey(Photo, null=True, on_delete=models.SET_NULL)
    
    category = models.CharField(max_length=100)
    description = models.CharField(max_length=1000)
    start_date = models.DateTimeField()
    end_date = models.DateTimeField()
    
class Review(models.Model):
    reviewer = models.ForeignKey(AbstractUser, related_name='reviewer')
    reviewee = models.ForeignKey(AbstractUser, related_name='reviewee')
    media = models.ForeignKey('Update', related_name='media', null=True)
    
    content = models.CharField(max_length=1000)
    date = models.DateTimeField()
    report_count = models.IntegerField()
    
class Schedule(models.Model):
    owner = models.ForeignKey(AbstractUser)
    
    rows = models.IntegerField()
    columns = models.IntegerField()
    data = models.CharField(max_length=1000)
    
class Update(models.Model):
    owner = models.ForeignKey(User)
    
    content = models.CharField(max_length=1000)
    pub_date = models.DateTimeField()
    mp3_url = models.CharField(max_length=300)
    mp3_name = models.CharField(max_length=300)
    video_id = models.CharField(max_length=300)
    fav = models.BooleanField()
    other = models.BooleanField()
    admin_update = models.BooleanField()
    
    def __unicode__(self):
        if self.content:
            return self.content
        elif self.video_id:
            return self.video_id
        elif self.mp3_url:
            return self.mp3_url
        else:
            return "Empty update!(possible error, check)"
    
class Thread(models.Model):
    receivers = models.ManyToManyField(AbstractUser)
    
    category = models.CharField(max_length=50)
    subject = models.CharField(max_length=100)
    date_started = models.DateTimeField()
    admin = models.BooleanField()

class ThreadNotification(models.Model):
    thread = models.ForeignKey(Thread)
    abstract_user = models.ForeignKey(AbstractUser)
    
    notifications = models.IntegerField()
    
class MSG(models.Model):
    thread = models.ForeignKey(Thread)
    sender = models.ForeignKey(AbstractUser, null=True)
    
    content = models.CharField(max_length=1000)
    date_sent = models.DateTimeField()
    
class LearningCircle(models.Model):
    thread = models.OneToOneField(Thread)
    
    start_date = models.DateTimeField()
    end_date = models.DateTimeField()
    place = models.CharField(max_length=500)
    
class Questionnaire(models.Model):
    abstract_user = models.OneToOneField(AbstractUser)
    
class Question(models.Model):
    questionnaire = models.ForeignKey(Questionnaire)
    
    content = models.CharField(max_length=300)
    
    def __unicode__(self):
        return self.content

times = (('12:00 am', '12:00 am'), ('12:10 am', '12:10 am'), ('12:20 am', '12:20 am'), ('12:30 am', '12:30 am'), 
         ('12:40 am', '12:40 am'), ('12:50 am', '12:50 am'), ('01:00 am', '01:00 am'), ('01:10 am', '01:10 am'), 
         ('01:20 am', '01:20 am'), ('01:30 am', '01:30 am'), ('01:40 am', '01:40 am'), ('01:50 am', '01:50 am'), 
         ('02:00 am', '02:00 am'), ('02:10 am', '02:10 am'), ('02:20 am', '02:20 am'), ('02:30 am', '02:30 am'), 
         ('02:40 am', '02:40 am'), ('02:50 am', '02:50 am'), ('03:00 am', '03:00 am'), ('03:10 am', '03:10 am'), 
         ('03:20 am', '03:20 am'), ('03:30 am', '03:30 am'), ('03:40 am', '03:40 am'), ('03:50 am', '03:50 am'), 
         ('04:00 am', '04:00 am'), ('04:10 am', '04:10 am'), ('04:20 am', '04:20 am'), ('04:30 am', '04:30 am'), 
         ('04:40 am', '04:40 am'), ('04:50 am', '04:50 am'), ('05:00 am', '05:00 am'), ('05:10 am', '05:10 am'), 
         ('05:20 am', '05:20 am'), ('05:30 am', '05:30 am'), ('05:40 am', '05:40 am'), ('05:50 am', '05:50 am'), 
         ('06:00 am', '06:00 am'), ('06:10 am', '06:10 am'), ('06:20 am', '06:20 am'), ('06:30 am', '06:30 am'), 
         ('06:40 am', '06:40 am'), ('06:50 am', '06:50 am'), ('07:00 am', '07:00 am'), ('07:10 am', '07:10 am'), 
         ('07:20 am', '07:20 am'), ('07:30 am', '07:30 am'), ('07:40 am', '07:40 am'), ('07:50 am', '07:50 am'), 
         ('08:00 am', '08:00 am'), ('08:10 am', '08:10 am'), ('08:20 am', '08:20 am'), ('08:30 am', '08:30 am'), 
         ('08:40 am', '08:40 am'), ('08:50 am', '08:50 am'), ('09:00 am', '09:00 am'), ('09:10 am', '09:10 am'), 
         ('09:20 am', '09:20 am'), ('09:30 am', '09:30 am'), ('09:40 am', '09:40 am'), ('09:50 am', '09:50 am'), 
         ('10:00 am', '10:00 am'), ('10:10 am', '10:10 am'), ('10:20 am', '10:20 am'), ('10:30 am', '10:30 am'), 
         ('10:40 am', '10:40 am'), ('10:50 am', '10:50 am'), ('11:00 am', '11:00 am'), ('11:10 am', '11:10 am'), 
         ('11:20 am', '11:20 am'), ('11:30 am', '11:30 am'), ('11:40 am', '11:40 am'), ('11:50 am', '11:50 am'),
         ('12:00 pm', '12:00 pm'), ('12:10 pm', '12:10 pm'), ('12:20 pm', '12:20 pm'), ('12:30 pm', '12:30 pm'), 
         ('12:40 pm', '12:40 pm'), ('12:50 pm', '12:50 pm'), ('01:00 pm', '01:00 pm'), ('01:10 pm', '01:10 pm'), 
         ('01:20 pm', '01:20 pm'), ('01:30 pm', '01:30 pm'), ('01:40 pm', '01:40 pm'), ('01:50 pm', '01:50 pm'), 
         ('02:00 pm', '02:00 pm'), ('02:10 pm', '02:10 pm'), ('02:20 pm', '02:20 pm'), ('02:30 pm', '02:30 pm'), 
         ('02:40 pm', '02:40 pm'), ('02:50 pm', '02:50 pm'), ('03:00 pm', '03:00 pm'), ('03:10 pm', '03:10 pm'), 
         ('03:20 pm', '03:20 pm'), ('03:30 pm', '03:30 pm'), ('03:40 pm', '03:40 pm'), ('03:50 pm', '03:50 pm'), 
         ('04:00 pm', '04:00 pm'), ('04:10 pm', '04:10 pm'), ('04:20 pm', '04:20 pm'), ('04:30 pm', '04:30 pm'), 
         ('04:40 pm', '04:40 pm'), ('04:50 pm', '04:50 pm'), ('05:00 pm', '05:00 pm'), ('05:10 pm', '05:10 pm'), 
         ('05:20 pm', '05:20 pm'), ('05:30 pm', '05:30 pm'), ('05:40 pm', '05:40 pm'), ('05:50 pm', '05:50 pm'), 
         ('06:00 pm', '06:00 pm'), ('06:10 pm', '06:10 pm'), ('06:20 pm', '06:20 pm'), ('06:30 pm', '06:30 pm'), 
         ('06:40 pm', '06:40 pm'), ('06:50 pm', '06:50 pm'), ('07:00 pm', '07:00 pm'), ('07:10 pm', '07:10 pm'), 
         ('07:20 pm', '07:20 pm'), ('07:30 pm', '07:30 pm'), ('07:40 pm', '07:40 pm'), ('07:50 pm', '07:50 pm'), 
         ('08:00 pm', '08:00 pm'), ('08:10 pm', '08:10 pm'), ('08:20 pm', '08:20 pm'), ('08:30 pm', '08:30 pm'), 
         ('08:40 pm', '08:40 pm'), ('08:50 pm', '08:50 pm'), ('09:00 pm', '09:00 pm'), ('09:10 pm', '09:10 pm'), 
         ('09:20 pm', '09:20 pm'), ('09:30 pm', '09:30 pm'), ('09:40 pm', '09:40 pm'), ('09:50 pm', '09:50 pm'), 
         ('10:00 pm', '10:00 pm'), ('10:10 pm', '10:10 pm'), ('10:20 pm', '10:20 pm'), ('10:30 pm', '10:30 pm'), 
         ('10:40 pm', '10:40 pm'), ('10:50 pm', '10:50 pm'), ('11:00 pm', '11:00 pm'), ('11:10 pm', '11:10 pm'), 
         ('11:20 pm', '11:20 pm'), ('11:30 pm', '11:30 pm'), ('11:40 pm', '11:40 pm'), ('11:50 pm', '11:50 pm')) 

class Slot(models.Model):
    abstract_user = models.ForeignKey(AbstractUser)
    
    title = models.CharField(max_length=100, blank=True)
    start = models.CharField(max_length=8, choices=times)
    end = models.CharField(max_length=8, choices=times)
    day = models.CharField(max_length=9, choices=(('Sunday', 'Sun'), ('Monday', 'Mon'),
                                                   ('Tuesday', 'Tue'), ('Wednesday', 'Wed'),
                                                   ('Thursday', 'Thurs'), ('Friday', 'Fri'),
                                                   ('Saturday', 'Sat')))
    location = models.CharField(max_length=200, blank=True)

class ActivationCode(models.Model):
    abstract_user = models.ForeignKey(AbstractUser, null=True)

    code = models.CharField(max_length=5)
    def __unicode__(self):
        if self.abstract_user:
            return self.abstract_user.user.username + " used " + self.code
        else:
            return self.code
class SlotForm(ModelForm):
    class Meta:
        model = Slot
        exclude = ('abstract_user')
  
class StudentForm(ModelForm):
    class Meta:
        model = Student   
        fields = ('dob', 'gender', 'experience', 'favourite_genre', 'instrument_played', 'jamming_style')
        widgets = {
            'experience': Textarea(),
        }
        
class TeacherForm(ModelForm):
    class Meta:
        model = Teacher
        fields = ('dob', 'gender', 'experience', 'favourite_genre', 'instrument_played', 'jamming_style', 'teaches')
        widgets = {
            'experience': Textarea(),
            'teaches': Textarea(),
        }
    
class InstituteForm(ModelForm):
    class Meta:
        model = Institute
        fields = ('about', 'address', 'open_on', 'open_from', 'open_to', 'contact_info')
        widgets = {
            'about': Textarea(),
            'contact_info': Textarea(),

        }
        
class MyUserChangeFormName(UserChangeForm):
    class Meta(UserChangeForm.Meta):
        fields = ('username', 'first_name', 'last_name', 'email')
    def clean_password(self):
        return ""
        
class MyUserChangeForm(UserChangeForm):
    class Meta(UserChangeForm.Meta):
        fields = ('username', 'email')
    def clean_password(self):
        return ""
